package com.mxnet.chongjin.nci.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mxnet.chongjin.nci.R;
import com.mxnet.chongjin.nci.base.BaseActivity;
import com.mxnet.chongjin.nci.bean.Inspection;
import com.mxnet.chongjin.nci.config.Api;
import com.mxnet.chongjin.nci.utils.StringUtils;
import com.mxnet.chongjin.nci.view.ItemInspectionCheckView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class PreInspectionListActivity extends BaseActivity {
    private Context mContext;
    private Button mButtonQuit, mButtonClear, mButtonQueryEqual, mButtonQueryInequal;
    private TextView mTextViewCheckSum, mTextViewPreinSum;
    private LinearLayout mLayout;
    private List<Inspection> mInspectionList;
    private SharedPreferences mPreferences;
    private int filter = 0;  //0全部列出    1  相同   -1  不同

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pre_inspection);
        mContext = PreInspectionListActivity.this;
        mPreferences = getPreferences();

        mInspectionList = new ArrayList<>();

        mTextViewCheckSum = findViewById(R.id.textview_pre_inspection_check_sum);

        mTextViewPreinSum = findViewById(R.id.textview_pre_inspection_prein_sum);

        mLayout = findViewById(R.id.ll_pre_inspection_list);
        initView();
        mLayout.setClickable(true);

        mButtonQueryEqual = findViewById(R.id.btn_pre_inspection_query_equal);
        mButtonQueryEqual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // todo 查询未入库且检查数量与预入数量一致的记录
                filter = 1;
                initView();
            }
        });

        mButtonQueryInequal = findViewById(R.id.btn_pre_inspection_query_inequal);
        mButtonQueryInequal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // todo 查询未入库且检查数量与预入数量不一致的记录
                filter = -1;
                initView();
            }
        });

        mButtonQuit = findViewById(R.id.btn_pre_inspection_quit);
        mButtonQuit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, IndexActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
            }
        });

        mButtonClear = findViewById(R.id.btn_pre_inspection_clear);
        mButtonClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTextViewPreinSum.setText("0");
                mLayout.removeAllViews();
                mInspectionList.clear();

            }
        });
    }

    public void initView() {
        // 查询所有未入库，该接口不传lot参数
        OkHttpUtils.get()
                .url(mPreferences.getString("URL_HEADER", "") + Api.QUERY_NOT_INSPECTION)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("*****==onError==", e.getMessage());
                    }

                    /**
                     * @param response
                     * @param id
                     */
                    @Override
                    public void onResponse(String response, int id) {
                        if (!TextUtils.isEmpty(response)) {
                            try {
                                JSONObject jsonObject = new JSONObject(response);
                                boolean isSuccess = jsonObject.optBoolean("success");
                                if (isSuccess) {
                                    mLayout.removeAllViews();
                                    JSONArray datas = jsonObject.optJSONArray("data");
                                    int preinCount = 0; // 记录预入数量
                                    int checkCount = 0;
                                    for (int i = 0; i < datas.length(); i++) {
                                        JSONObject object = datas.optJSONObject(i);
                                        // 记录预入数量
                                        int inqty = StringUtils.toInt(object, "inqty");
                                        int checkqty = StringUtils.toInt(object, "checkqty");
                                        if ((filter == 1 && inqty != checkqty) || (filter == -1 && inqty == checkqty)) {
                                            continue;
                                        }
                                        preinCount += inqty;
                                        checkCount += checkqty;
                                        // 数据采集
                                        Inspection inspection = new Inspection();
                                        inspection.setId(object.optInt("id"));
                                        inspection.setLot(object.optString("lot"));
                                        inspection.setLocationCode(object.optString("location"));
                                        inspection.setInQty(object.optInt("inqty"));
                                        mInspectionList.add(inspection);
                                        // 添加视图
                                        ItemInspectionCheckView view = new ItemInspectionCheckView(mContext, R.layout.item_inspection_check);
                                        view.getTextView1().setText(object.optString("lot"));
                                        view.getTextView2().setText(object.optString("location"));
                                        view.getTextView3().setText(object.isNull("checkqty") ? "0" : object.getInt("checkqty") + "");
                                        view.getTextView4().setText(TextUtils.isEmpty(object.optInt("inqty") + "") ? "0" : object.optInt("inqty") + "");
                                        view.getTextView4().setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Log.i("*****4onClick", "4onClick");
                                                ItemInspectionCheckView view = (ItemInspectionCheckView) v.getParent().getParent();
                                                Intent intent = new Intent(mContext, StockCheckActivity.class);
                                                intent.putExtra("lot", view.getTextView1().getText().toString());
                                                mContext.startActivity(intent);
                                            }
                                        });
                                        // 跳转该批次的仓库数量核对界面
                                        view.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                Log.i("*****onClick", "onClick");
//                                                Intent intent = new Intent(mContext, StockCheckActivity.class);
//                                                intent.putExtra("lot", view.getTextView1().getText().toString());
//                                                mContext.startActivity(intent);
                                            }
                                        });
                                        mLayout.addView(view);
                                    }
                                    mTextViewPreinSum.setText(preinCount + "");
                                    mTextViewCheckSum.setText(checkCount + "");
                                } else {
                                    Log.i("*****onResponse", jsonObject.optString("message") + "\n" + response);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }
}
